package com.hmy.finance.service.core.dto;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmy.finance.api.bo.core.request.ContractTerminationStopFlowAndOffsetRequest;
import com.hmy.finance.common.constants.CommonConstants;
import com.hmy.finance.dal.po.ContractTerminationStopRecordPo;
import com.hmy.finance.dal.po.DeviceRentalRecordPo;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 合同终止-停租流水和冲抵
 *
 * @author songkf
 */
@Data
public class ContractTerminationStopFlowAndOffsetDto implements java.io.Serializable {

    private static final long serialVersionUID = -6255976882350212603L;
    /**
     * 合同ID
     */
    private Long contractId;
    /**
     * 退场单ID
     */
    private Long exitFormId;
    /**
     * 发起时间
     */
    private java.time.LocalDateTime startTime;
    /**
     * 销售经理ID
     */
    private Long salesManagerId;
    /**
     * 销售经理名称
     */
    private String salesManagerName;

    //=============== Logic Process Parameters ===============
    private ContractTerminationStopRecordPo contractTerminationStopRecordPo;

    /**
     * 合同下的所有在租设备集合
     */
    private List<DeviceRentalRecordPo> deviceRentalRecordPoList;

    /**
     * 构建请求参数
     *
     * @param request 请求
     * @return 结果
     */
    public static ContractTerminationStopFlowAndOffsetDto buildFromRequest(ContractTerminationStopFlowAndOffsetRequest request) {
        ContractTerminationStopFlowAndOffsetDto dto = new ContractTerminationStopFlowAndOffsetDto();
        dto.setContractId(request.getContractId());
        dto.setExitFormId(request.getExitFormId());
        dto.setStartTime(request.getStartTime());
        dto.setSalesManagerId(request.getSalesManagerId());
        dto.setSalesManagerName(request.getSalesManagerName());
        return dto;
    }

    public Wrapper<DeviceRentalRecordPo> getDeviceRentalRecordPoListQueryWrapper() {
        return Wrappers.lambdaQuery(DeviceRentalRecordPo.class)
                .eq(DeviceRentalRecordPo::getContractId, contractId)
                .eq(DeviceRentalRecordPo::getIsDeleted, CommonConstants.NOT_DELETED);
    }

    public void buildContractTerminationStopFlowAndOffsetRecord(DefaultIdentifierGenerator identifierGenerator) {
        contractTerminationStopRecordPo = new ContractTerminationStopRecordPo();
        contractTerminationStopRecordPo.setContractId(contractId);
        contractTerminationStopRecordPo.setExitFormId(exitFormId);
        contractTerminationStopRecordPo.setStartTime(startTime);
        contractTerminationStopRecordPo.setSalesManagerId(salesManagerId);
        contractTerminationStopRecordPo.setSalesManagerName(salesManagerName);
        contractTerminationStopRecordPo.setStatus(CommonConstants.CONTRACT_TERMINATION_STOP_RECORD_STATUS_STOPPING);
        contractTerminationStopRecordPo.setIsDeleted(CommonConstants.NOT_DELETED);
        contractTerminationStopRecordPo.setCreateTime(LocalDateTime.now());
        contractTerminationStopRecordPo.setUpdateTime(LocalDateTime.now());
        contractTerminationStopRecordPo.setCreatorId(-1L);
        contractTerminationStopRecordPo.setUpdaterId(-1L);
        contractTerminationStopRecordPo.setCreatorName("system");
        contractTerminationStopRecordPo.setUpdaterName("system");
        contractTerminationStopRecordPo.setId(identifierGenerator.nextId(contractTerminationStopRecordPo));

    }

    public void assembleDeviceRentalRecordUpdateInfo() {
        deviceRentalRecordPoList.forEach(deviceRentalRecordPo -> {
            deviceRentalRecordPo.setFlowFlag(2);
            deviceRentalRecordPo.setOffsetFlag(2);
        });
    }
}
